导航菜单
首页 >  c语言 考研  > 王道C语言督学营OJ练习全解【24考研最新版】

王道C语言督学营OJ练习全解【24考研最新版】

前言

本篇博客是在博主参加王道408专业课前置课程-----C语言督学营的学习笔记,包含了从第一节课到最后一节课的所有OJ习题题解,文章中每一题都给出了详尽的代码,并在每一题的关键部位加上了注释,记录下来的目的是方便自己以后进行复习,也希望能够帮助到大家,经过测试所有代码都可以通过王道OJ网站给出的样例,如果还有什么疑问的话评论区留言吧!(tips:网站上的样例比较简单,可能刷刷小聪明也可以通过,但是我建议大家还是按照要求来最好,毕竟是要锻炼自己的能力,投机取巧也不好!如果是感觉非常没有必要的能省则省。)

总体来说C语言督学营不算难,讲的知识很贴切考研考的知识点,前期主要对C语言语法进行了讲解,中后期补充了一些有关408的专业课知识点:

初级阶段C语言基础语法数据类型&数据的输入输出运算符与表达式选择循环一维数组与字符串指针函数结构体与C++引用 中级阶段简要介绍了C语言数据结构顺序表基础操作单链表基础操作栈与队列基础操作二叉树层次建树&层序遍历前中后序遍历&求WPL顺序&二分&二叉排序树(查找算法)冒泡&选择&插入&归并&快速&堆(排序算法)另加一些数据结构相关的考研大题 高级阶段C语言语法补充(大多是考研不考的语法,或者有更好的替代)数据的机器级表示(计组里面的一些基本概念&IEEE754浮点数表示法)汇编语言入门C语言文件另加一些计算机组成原理相关的大题

文章目录初级阶段课时二作业1作业2作业3 课时三作业1作业2 课时四作业1作业2作业3 课时五作业1作业2 课时六作业1作业2 课时七课时八作业1作业2中级阶段课时十课时十一课时十二课时十三课时十四作业1作业2 课时十五课时十六课时十七作业1作业2高级阶段课时十九作业1作业2 课时二十课时二十一

初级阶段 课时二 作业1

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include int main() {printf("hello wangdao");return 0;}

注意点

注意输入输出格式即可!HelloWord!

作业2

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include int main() {int a,b;scanf("%d %d",&a,&b);printf("%d",a+b);return 0;}

注意点

这个题目没有坑,就是体验一下C语言语法,没有涉及到大数加法!

作业3

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include int main() {int a;scanf("%d",&a);printf("%c",a);return 0;}

注意点

旨在锻炼学生ASCII值与字符之间的转化!这点在考研时挺重要的!

课时三 作业1

题目描述

在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include int main() {int i;scanf("%d",&i);if (i%4==0&&i%100!=0||i%400==0){printf("yes");}else{printf("no");}return 0;}

注意点

闰年应该是能被4整除不能被100整除或者能被400整除的年份!

作业2

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include int main() {int i;char j;float k;scanf("%d %c %f",&i,&j,&k);printf("%.2f",i+j+k);return 0;}

注意点

考察格式化输出!

课时四 作业1

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include int main() {int i,j;scanf("%d",&i);j=i;int ans=0;while (i!=0) {ans = ans * 10 + i % 10;i /= 10;}if(j==ans){printf("yes");}else{printf("no");}return 0;}

注意点

考察循环语法的使用。

作业2

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include int main() {int i,ans=1;scanf("%d",&i);for (int k=1;knext=q;return true;}else{return false;}}void print_list(myList *head);myList* index_search(myList *head,int i){if(inext!=NULL&&jnext;}if(p->next==NULL){printf("false");}else{myList *q=p->next;p->next=p->next->next;print_list(head);free(q);}}//遍历链表void print_list(myList *head){for (myList *p=head;p->next!=NULL;p=p->next){printf("%3d",p->next->data);}printf("\n");}int main() {// 创建一个头结点(头结点一般不用于存储信息)myList *head=(myList*)malloc(sizeof (myList));head->next=NULL;random_insert(head,1,1);random_insert(head,2,2);random_insert(head,3,3);//print_list(head);int n;scanf("%d",&n);random_insert(head,2,n);print_list(head);scanf("%d",&n);index_search(head,n);return 0;}

注意点

这个题目看着代码很长自己一定要会敲!!!很重要很重要!

课时十一

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include #include#define bool int#define true 1#define false 0typedef int ElemType;//数组类型struct myList{ElemType data;struct myList* next;};typedef struct myList myList;void print_list(myList *head);//头插法void head_insert(myList* head,ElemType e){myList *p= (myList*)malloc(sizeof (myList));p->data=e;p->next=head->next;head->next=p;// 如果写成以下样子,将不会有效果// head->next=p->next;}//尾插法void tail_insert(myList* tail,ElemType e){myList *p;p=tail;while (p->next!=NULL){p=p->next;}p->next=(myList*) malloc(sizeof (myList));p->next->data=e;p->next->next=NULL;}//遍历链表void print_list(myList *head){for (myList *p=head;p->next!=NULL;p=p->next){printf("%-2d",p->next->data);}printf("\n");}void PrintList(myList* L){L=L->next;while(L!=NULL){printf("%d",L->data);//打印当前结点数据L=L->next;//指向下一个结点if(L!=NULL){printf(" ");}}printf("\n");}int main() {// 创建一个头结点(头结点一般不用于存储信息)myList *head1=(myList*)malloc(sizeof (myList));head1->next=NULL;myList *head2=(myList*)malloc(sizeof (myList));head2->next=NULL;int i=-1;while(1){scanf("%d",&i);if(i==9999){break;}head_insert(head1,i);}i=-1;while(1){scanf("%d",&i);if(i==9999){break;}tail_insert(head2,i);}//print_list(head1);//print_list(head2);PrintList(head1);PrintList(head2);return 0;}

注意点

这里要注意的是头插法与尾插创建的链表在打印时是不一样的,一个是倒序打印一个是顺序打印!

课时十二

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include #include#define bool int#define true 1#define false 0typedef int ElemType;//数组类型struct myList{ElemType data;struct myList* next;};typedef struct myList myList;void print_list(myList *head);//尾插法void tail_insert(myList* tail,ElemType e){myList *p;p=tail;while (p->next!=NULL){p=p->next;}p->next=(myList*) malloc(sizeof (myList));p->next->data=e;p->next->next=NULL;}//随机插入bool random_insert(myList* head,int i,ElemType e){myList *p=head;int j=0;for(;p!=NULL,jnext;}if(p!=NULL){myList *q=(myList*)malloc(sizeof (myList));q->data=e;q->next=p->next;p->next=q;return true;}else{return false;}}//按位置查找myList* index_search(myList *head,int i){if(inext!=NULL&&jnext;}return p->next;}//删除相应位置元素bool delete_i(myList *head,int i){if(inext!=NULL&&jnext;}if(p->next==NULL){printf("false");}else{myList *q=p->next;p->next=p->next->next;print_list(head);free(q);}}//遍历链表void print_list(myList *head){if(head->next==NULL){printf("null!!!");}for (myList *p=head;p->next!=NULL;p=p->next){printf("%3d",p->next->data);}printf("\n");}void print(myList *p){if(p!=NULL){printf("%d\n",p->data);}else{printf("null\n");}}int main() {// 创建一个头结点(头结点一般不用于存储信息)myList *head=(myList*)malloc(sizeof (myList));head->next=NULL;int n;while (1){scanf("%d",&n);if (n==9999){break;}tail_insert(head,n);}print(index_search(head,2));random_insert(head,2,99);print_list(head);delete_i(head,4);return 0;}

注意点

如果前面两个掌握了那么这个题目难不住!

课时十三

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include #include#define MaxSize 5#define bool int#define true 1#define false 0typedef int ElemType;//数组类型struct myList{ElemType data[MaxSize];int top;};typedef struct myList myList;bool insert_stack(myList *stack,ElemType e){if (stack->top==MaxSize-1){return false;}stack->data[++stack->top]=e;return true;}ElemType pop_stack(myList *stack){if(stack->top==-1){return false;}return stack->data[stack->top--];}struct myList1{ElemType data[MaxSize];int font;int tail;int maxsize};typedef struct myList1 myList1;//这样写,最后一个位置是留空的bool push_queue(myList1 *tail,ElemType e){if((tail->tail+1)%tail->maxsize==tail->font){printf("false\n");return false;}tail->data[tail->tail]=e;tail->tail=(tail->tail+1)%tail->maxsize;return true;}ElemType pop_queue(myList1 *queue){if(queue->tail==queue->font){return false;}ElemType e;e=queue->data[queue->font];queue->font=(queue->font+1)%queue->maxsize;return e;}int main() {myList *stack=(myList*)malloc(sizeof (myList));stack->top=-1;int a;for(int i;itop!=-1){printf(" %d", pop_stack(stack));}printf("\n");myList1 *queue=(myList1*)malloc(sizeof (myList1));queue->font=queue->tail=0;queue->maxsize=MaxSize;for(int i=0;ifont!=queue->tail){printf(" %d", pop_queue(queue));}return 0;} 课时十四 作业1

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include #include //基础元素类型#define ElementData char#define ElementDataEnd '\n'struct BiTNode{ElementData data;struct BiTNode *leftNode;struct BiTNode *rightNode;};typedef struct BiTNode BiTNode;//辅助队列(通过这个队列层次建树)struct auxiliary{// 指向二叉树中的节点struct BiTNode* p;struct auxiliary *next;};typedef struct auxiliary tagQueue;//封装函数初始化一个根节点BiTNode* newNode(ElementData e){BiTNode *pnew=(BiTNode*) malloc(sizeof (BiTNode));pnew->leftNode=NULL;pnew->rightNode=NULL;pnew->data=e;return pnew;}tagQueue* newTagNode(BiTNode* pnew){tagQueue *p=(tagQueue *) malloc(sizeof (tagQueue));p->next=NULL;p->p=pnew;return p;}// -------------------层序建树------------------------------//BiTNode *Sequence_tree_building(){BiTNode *tree=NULL,*pnew=NULL;tagQueue *tail=NULL,*tagQ=NULL,*p;ElementData e;while(scanf("%c",&e)){if(e==ElementDataEnd){break;}//将信息写入新生成的节点pnew=newNode(e);p=newTagNode(pnew);// 写成NULL==tail是为了防止tail=NULL的情况发生;//改变指针之间的关系,将节点插入相应位置if(NULL==tree){//此分支创建二叉树,创建辅助队列tree=pnew;tail=p;tagQ=tail;continue;}else{//将新节点添加到队尾tail->next=p;tail=tail->next;}// 精妙之处,通过辅助队列构建二叉树if(tagQ->p->leftNode==NULL){tagQ->p->leftNode=pnew;}else if(tagQ->p->rightNode==NULL){// 此分支多一句是因为,这种建树方式是层序建树(这颗子树左右都有的话会向兄弟树根偏移,而辅助队列下一个就是本层的兄弟树根)tagQ->p->rightNode=pnew;tagQ=tagQ->next;}}return tree;}// ---------------先序遍历----------------//void Pre_Consult(BiTNode* tree){if(tree==NULL){return;}printf("%c",tree->data);Pre_Consult(tree->leftNode);Pre_Consult(tree->rightNode);}// ---------------------------------------------------------------------------------//int main() {BiTNode *tree=Sequence_tree_building();// 前序遍历Pre_Consult(tree);printf("\n");return 0;} 作业2

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include #include //基础元素类型#define ElementData char#define ElementDataEnd '\n'struct BiTNode{ElementData data;struct BiTNode *leftNode;struct BiTNode *rightNode;};typedef struct BiTNode BiTNode;//辅助队列(通过这个队列层次建树)struct auxiliary{// 指向二叉树中的节点struct BiTNode* p;struct auxiliary *next;};typedef struct auxiliary tagQueue;//封装函数初始化一个根节点BiTNode* newNode(ElementData e){BiTNode *pnew=(BiTNode*) malloc(sizeof (BiTNode));pnew->leftNode=NULL;pnew->rightNode=NULL;pnew->data=e;return pnew;}tagQueue* newTagNode(BiTNode* pnew){tagQueue *p=(tagQueue *) malloc(sizeof (tagQueue));p->next=NULL;p->p=pnew;return p;}// -------------------层序建树------------------------------//BiTNode *Sequence_tree_building(){BiTNode *tree=NULL,*pnew=NULL;tagQueue *tail=NULL,*tagQ=NULL,*p;ElementData e;while(scanf("%c",&e)){if(e==ElementDataEnd){break;}//将信息写入新生成的节点pnew=newNode(e);p=newTagNode(pnew);// 写成NULL==tail是为了防止tail=NULL的情况发生;//改变指针之间的关系,将节点插入相应位置if(NULL==tree){//此分支创建二叉树,创建辅助队列tree=pnew;tail=p;tagQ=tail;continue;}else{//将新节点添加到队尾tail->next=p;tail=tail->next;}// 精妙之处,通过辅助队列构建二叉树if(tagQ->p->leftNode==NULL){tagQ->p->leftNode=pnew;}else if(tagQ->p->rightNode==NULL){// 此分支多一句是因为,这种建树方式是层序建树(这颗子树左右都有的话会向兄弟树根偏移,而辅助队列下一个就是本层的兄弟树根)tagQ->p->rightNode=pnew;tagQ=tagQ->next;}}return tree;}void Layer_Consult(BiTNode* tree){tagQueue *head=(tagQueue*) malloc(sizeof (tagQueue)),*tail,*q;head->p=tree;head->next=NULL;tail=head;while (head!=NULL){printf("%c",head->p->data);//入队if(head->p->leftNode!=NULL){tagQueue *p=(tagQueue*) malloc(sizeof (tagQueue));p->p=head->p->leftNode;p->next=NULL;tail->next=p;tail=p;}if(head->p->rightNode!=NULL){tagQueue *p=(tagQueue*) malloc(sizeof (tagQueue));p->p=head->p->rightNode;p->next=NULL;tail->next=p;tail=p;}q=head;head=head->next;free(q);q=NULL;}}// ---------------------------------------------------------------------------------//// ---------------中序遍历----------------//void Middle_Consult(BiTNode* tree){if(tree==NULL){return;}Middle_Consult(tree->leftNode);printf("%c",tree->data);Middle_Consult(tree->rightNode);}// ---------------后序遍历----------------//void Last_Consult(BiTNode* tree){if(tree==NULL){return;}Last_Consult(tree->leftNode);Last_Consult(tree->rightNode);printf("%c",tree->data);}int main() {BiTNode *tree=Sequence_tree_building();// 前序遍历Middle_Consult(tree);printf("\n");Last_Consult(tree);printf("\n");Layer_Consult(tree);printf("\n");return 0;} 课时十五

题目描述 在这里插入图片描述

AC代码

//// Created by Zhu Shichong on 2023/1/9.//#include #include #define ElementData int#define ElementDataEnd -1//二叉树的节点struct BiTNode{ElementData data;struct BiTNode *leftNode;struct BiTNode *rightNode;};typedef struct BiTNode BiTNode;#define bool int#define true 1#define false 0#define MaxSize 10//初始化二叉排序树----------循环实现方式BiTNode *Creat_BST(ElementData *a,int len){BiTNode *tree=(BiTNode*)malloc(sizeof (BiTNode));tree->data=a[0];tree->rightNode=tree->leftNode=NULL;for(int i=1;idata=a[i];q->leftNode=q->rightNode=NULL;// 用于保留p指针的位置BiTNode *prep=NULL;while (p!=NULL){prep=p;if(p->data>q->data){p=p->leftNode;}else{p=p->rightNode;}}// 将值插入找出的位置;prep->data > q->data?(prep->leftNode = q):(prep->rightNode = q);// 下面这种写法是错误的// prep->data > q->data?prep->leftNode = q:prep->rightNode = q;}return tree;}//二分查找int Bin_Search(int *a,int len,int e){int l,r,middle;l=0;r=len-1;while (le){// 这里之所以这么确定,是因为middle指向的地方不是我们所要的值r=middle-1;}else{l=middle+1;}}return -1;}void PrintBST(BiTNode *tree,ElementData *d,int *p){if(NULL==tree){return;}PrintBST(tree->leftNode,d,p);d[(*p)++]=tree->data;printf("%3d",tree->data);PrintBST(tree->rightNode,d,p);}int main() {BiTNode *tree=NULL;ElementData a[MaxSize]={22,1,33,13,45,31,65,221,857,3};ElementData b[MaxSize];//用于接收二叉排序树中序遍历的值for (int i=0;i

相关推荐: